﻿using System;
using System.Text;

namespace BLL
{
    public static class Utility
    {
        /// <summary>
        /// 返回距离现在的时间
        /// </summary>
        /// <param name="time">要计算的时间</param>
        /// <returns>计算后的字符串</returns>
        public static string GetTimeSpan(DateTime time)
        {
            var span = DateTime.Now - time;
            if (span.TotalSeconds <= 59)
                return "数秒前";
            if (span.TotalMinutes <= 59)
                return string.Format("{0}分钟前", Math.Round(span.TotalMinutes));
            if (span.TotalHours <= 23)
                return string.Format("{0}小时前", Math.Round(span.TotalHours));
            if (span.TotalDays <= 30)
                return string.Format("{0}天前", Math.Round(span.TotalDays));
            return time.ToLongDateString();
        }

        /// <summary>
        /// 返回人人个人主页地址
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>主页地址</returns>
        public static string GetRenrenUrl(string id)
        {
            return string.Format("http://www.renren.com/profile.do?id={0}", id);
        }

        /// <summary>
        /// 处理浮点数，保留一位小数或显示10
        /// </summary>
        /// <param name="d">浮点数</param>
        /// <returns>处理后的字符串</returns>
        public static string Round(double d)
        {
            var str = d.ToString("F1");
            if (str == "0.0")
                return "0";
            if (str == "10.0")
                return "10";
            return str;
        }

        public static string ShortenClassName(string  s)
        {
            if(s.Length >= 9)
            {
                StringBuilder ss = new StringBuilder();
                ss.Append(s.Substring(0, 9));
                ss.Append("...");
                return ss.ToString();
            }
            return s;
        }

        public static string ShortenTeacherName(string s)
        {
            if(s.Length>=10)
            {
                StringBuilder ss = new StringBuilder();
                ss.Append(s.Substring(0, 10));
                ss.Append("...");
                return ss.ToString();
            }
            return s;
        }
        public static string FullName(String s)
        {
            StringBuilder ss = new StringBuilder();
            ss.Append("\"");
            ss.Append(s);
            ss.Append("\"");
            return ss.ToString();
        }
    }
}
